﻿using BookStore.Solution.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStore.Solution.BLL
{
    public class SupplierManager
    {
        public string Insert(Supplier entity)
        {
            Supplier supplier = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                try
                {
                    supplier = ctx.Suppliers.Where(a => a.SupplierID == entity.SupplierID).FirstOrDefault<Supplier>();
                    if (supplier == null)
                    {
                        ctx.Suppliers.Add(entity);
                        if (ctx.SaveChanges() > 0)
                        {
                            SystemLogManager.Insert("Thêm NCC: " + entity);
                            return "Thêm thành công";
                        }
                        else
                            return "Thêm thất bại";
                    }
                    else
                        return "Trùng khóa chính";
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }
        }

        public string Update(Supplier entity)
        {
            Supplier supplier = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                try
                {
                    supplier = ctx.Suppliers.Where(a => a.SupplierID == entity.SupplierID).FirstOrDefault<Supplier>();
                    if (supplier != null)
                    {
                        supplier.SupplierName = entity.SupplierName;
                        supplier.Address = entity.Address;
                        supplier.Email = entity.Email;
                        supplier.PhoneNumber = entity.PhoneNumber;
                        supplier.Website = entity.Website;
                        ctx.Entry(supplier).State = System.Data.Entity.EntityState.Modified;
                        if (ctx.SaveChanges() > 0)
                        {
                            SystemLogManager.Insert("Cập nhật NCC: " + supplier);
                            entity = supplier;
                            return "Cập nhật thành công";
                        }
                        else
                            return "Cập nhật thất bại";
                    }
                    else
                        return "Mã nhà cung cấp không tồn tại";
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }
        }

        public string Delete(Supplier t)
        {
            Supplier supplier = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                supplier = ctx.Suppliers.Where(a => a.SupplierID == t.SupplierID).FirstOrDefault<Supplier>();
                if (supplier != null)
                {
                    supplier.Status = "Đã xóa";
                    ctx.Entry(supplier).State = System.Data.Entity.EntityState.Modified;
                    if (ctx.SaveChanges() > 0)
                    {
                        SystemLogManager.Insert("Xóa NCC: " + supplier);
                        return "Xóa thành công";
                    }
                    else
                        return "Xóa thất bại";
                }
                else
                    return "Mã nhà cung cấp không tồn tại";
            }
        }

        public DataTable SearchByName(string name)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                DataTable dt = CreateTable();
                var SupplierList = (from a in ctx.Suppliers where a.SupplierName.Contains(name) select a).ToList<Supplier>();
                foreach (var row in SupplierList)
                {
                    dt.LoadDataRow(CreateRow(row), false);
                }
                return dt;
            }
        }

        public DataTable SearchByID(string id)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                DataTable dt = CreateTable();
                var SupplierList = (from a in ctx.Suppliers where a.SupplierID == id select a).ToList<Supplier>();
                foreach (var row in SupplierList)
                {
                    dt.LoadDataRow(CreateRow(row), false);
                }
                return dt;
            }
        }

        private DataTable CreateTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("SupplierID", typeof(string));
            dt.Columns.Add("SupplierName", typeof(string));
            dt.Columns.Add("Address", typeof(string));
            dt.Columns.Add("Email", typeof(string));
            dt.Columns.Add("PhoneNumber", typeof(string));
            dt.Columns.Add("Website", typeof(string));
            return dt;
        }

        public DataTable GetAll()
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                DataTable dt = CreateTable();
                var SupplierList = (from a in ctx.Suppliers where a.Status != "Đã xóa" select a).ToList<Supplier>();
                foreach (var row in SupplierList)
                {
                    dt.LoadDataRow(CreateRow(row), false);
                }
                return dt;
            }
        }

        private object[] CreateRow(Supplier entity)
        {
            return new object[] { entity.SupplierID, entity.SupplierName, entity.Address, entity.Email, entity.PhoneNumber, entity.Website };
        }
    }
}
